package com.cskaoyan.service;

import com.cskaoyan.bean.MarketGoodsExample;
import com.cskaoyan.bean.MarketGoodsProductExample;
import com.cskaoyan.bean.MarketOrderExample;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketGoodsProductMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.HashMap;
import java.util.Map;

public class AdminDashboardServiceImpl implements AdminDashboardService {


    @Override
    public Map<Object, Object> getAllAdminDashboard() {
        MarketUserExample marketUserExample = new MarketUserExample();
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsProductExample marketGoodsProductExample = new MarketGoodsProductExample();

        SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MarketOrderMapper orderMapper = sqlSession.getMapper(MarketOrderMapper.class);
        long orderNum = orderMapper.countByExample(marketOrderExample);
        MarketUserMapper userMapper = sqlSession.getMapper(MarketUserMapper.class);
        long userNum = userMapper.countByExample(marketUserExample);

        MarketGoodsMapper goodsMapper = sqlSession.getMapper(MarketGoodsMapper.class);
        long goodsNum = goodsMapper.countByExample(marketGoodsExample);

        MarketGoodsProductMapper productMapper = sqlSession.getMapper(MarketGoodsProductMapper.class);
        long productNum = productMapper.countByExample(marketGoodsProductExample);

        HashMap <Object, Object> map = new HashMap <>();

        map.put("userTotal",  userNum);
        map.put("goodsTotal",goodsNum);
        map.put("orderTotal",orderNum);
        map.put("productTotal",productNum);






        if (sqlSession!=null){

            sqlSession.close();
        }



        return map;
    }
}
